package com.itcam.tingshu.order.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itcam.tingshu.common.login.TsLogin;
import com.itcam.tingshu.common.result.Result;
import com.itcam.tingshu.common.util.AuthContextHolder;
import com.itcam.tingshu.model.order.OrderInfo;
import com.itcam.tingshu.order.service.OrderInfoService;
import com.itcam.tingshu.vo.order.OrderInfoVo;
import com.itcam.tingshu.vo.order.TradeVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Tag(name = "订单管理")
@RestController
@RequestMapping("api/order/orderInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class OrderInfoApiController {

    @Autowired
    private OrderInfoService orderInfoService;

	/*
		{"itemType":"1003","itemId":1}
		{"itemType":"1003","itemId":2}
		{"itemType":"1003","itemId":1}
	 */

    /**
     * 订单结算页
     * @param tradeVo
     * @return
     */
    @Operation(summary = "订单结算页")
    @PostMapping("/trade")
    public Result trade(@RequestBody TradeVo tradeVo) {
        /*
            @Schema(description = "订单明细列表", required = true)
            private List<OrderDetailVo> orderDetailVoList;

            @Schema(description = "订单减免明细列表")
            private List<OrderDerateVo> orderDerateVoList;

            @Schema(description = "时间戳", required = true)
            private Long timestamp;

            @Schema(description = "签名", required = true)
            private String sign;
         */
        // 获取用户Id
        Long userId = AuthContextHolder.getUserId();
        // 调用服务层方法
        OrderInfoVo orderInfoVo = this.orderInfoService.trade(tradeVo, userId);
        // 返回数据
        return Result.ok(orderInfoVo);
    }

    /**
     * 提交订单
     * @param orderInfoVo
     * @return
     */
    @TsLogin
    @Operation(summary = "提交订单")
    @PostMapping("/submitOrder")
    public Result<Map<String, Object>> submitOrder(@RequestBody @Validated OrderInfoVo orderInfoVo) {
        //  获取到用户Id
        Long userId = AuthContextHolder.getUserId();
        //  调用服务层方法
        String orderNo = this.orderInfoService.submitOrder(orderInfoVo, userId);
        Map<String, Object> map = new HashMap<>();
        map.put("orderNo", orderNo);
        //	返回数据
        return Result.ok(map);
    }

    /**
     * 根据订单编号获取订单对象
     * @param orderNo
     * @return
     */
    @Operation(summary = "根据订单编号获取订单对象")
    @GetMapping("/getOrderInfo/{orderNo}")
    public Result getOrderInfo(@PathVariable String orderNo) {
        // 调用服务层方法
        OrderInfo orderInfo = this.orderInfoService.getOrderInfo(orderNo);
        // 返回数据
        return Result.ok(orderInfo);
    }

    /**
     * 查看我的订单
     * @param page
     * @param limit
     * @return
     */
    @TsLogin
    @Operation(summary = "获取用户订单")
    @GetMapping("findUserPage/{page}/{limit}")
    public Result findUserPage(
            @Parameter(name = "page", description = "当前页码", required = true)
            @PathVariable Long page,
            @Parameter(name = "limit", description = "每页记录数", required = true)
            @PathVariable Long limit,
            HttpServletRequest request) {
        String orderStatus = request.getParameter("orderStatus");
        // 获取到用户Id
        Long userId = AuthContextHolder.getUserId();
        Page<OrderInfo> pageParam = new Page<>(page, limit);
        // 调用服务层方法
        IPage<OrderInfo> pageModel = orderInfoService.findUserPage(pageParam, userId, orderStatus);
        return Result.ok(pageModel);
    }
}

















